


htmlTable = function (id,cols,rows,border,control_container) {
    var $_htmlTable = this;
    
    $_htmlTable.insertMode = false;
    
    $_htmlTable.context  = ["content_text"];
    
    $_htmlTable.next_id = function () {
        return "htmlTable_" + Math.floor(Math.random()*999999999)
    }
    
    $_htmlTable.id = (id ? id : $_htmlTable.next_id());
    $_htmlTable.cols = (cols ? cols : 5);
    $_htmlTable.rows = (rows ? rows : 5);
    $_htmlTable.border = (border ? border : 1);
    $_htmlTable.control_container = (control_container ? control_container : "htmlTable_controls");
    $_htmlTable.icon = "/modules/htmlTable/images/table_add.png";
    
    $_htmlTable.insertHtmlAtCursor = function (html) {
        if ($_htmlTable.insertMode) {
            var range, node;
            if (window.getSelection && window.getSelection().getRangeAt) {
                range = window.getSelection().getRangeAt(0);
                node = range.createContextualFragment(html);
                range.insertNode(node);
            } else if (document.selection && document.selection.createRange) {
                document.selection.createRange().pasteHTML(html);
            }
        } else alert ("This area does not accept tables");
    };  
    
    $_htmlTable.fill = function (id) {
        var tbody = $( "#" + id);
        
        var cols = $("#" + $_htmlTable.control_container + "_cols").val() ? $("#" + $_htmlTable.control_container + "_cols").val() : $_htmlTable.cols;
        if (cols==0) cols =  $_htmlTable.cols;
        var rows = $("#" + $_htmlTable.control_container + "_rows").val() ? $("#" + $_htmlTable.control_container + "_rows").val() : $_htmlTable.rows;
        if (rows==0) rows =  $_htmlTable.rows;
        if (tbody == null || tbody.length < 1) return;
        for (var r = 1; r <= rows; r++) {
               var trow = $("<tr>");
               for (var c = 1; c <= cols; c++) {
                   var html = "<td><span contentEditable>Cell " + r + "." + c + "</span></td>"
                   $(html).appendTo(trow);
              }
              trow.appendTo(tbody);
          }
     };
     
     $_htmlTable.create = function () { 
         var id = $_htmlTable.next_id ();
         if($("#" + $_htmlTable.control_container + "_grid").is(':checked')) $_htmlTable.border = 1;
         else $_htmlTable.border = 0;
         var html = "<table id=\"" + id + "\" border=\"" + $_htmlTable.border + "\" ><tbody></tbody></table>";
         $_htmlTable.insertHtmlAtCursor(html);
         $_htmlTable.fill(id);
     };
     
     $_htmlTable.controls = function () { 
         var html = "<strong>Insert Table:</stong><br>\
                    <input id=\"" + $_htmlTable.control_container + "_rows\" size=1 value=5> rows by \
                    <input id=\"" + $_htmlTable.control_container + "_cols\" size=1 value=5> columns <br>\
                    <input type=checkbox id=\"" + $_htmlTable.control_container + "_grid\" checked >Border<br>\
                    <img alt=\"Insert table\" title=\"Insert table\" id=btn_add_table src=\"" + $_htmlTable.icon + "\">";
         $("#" + $_htmlTable.control_container).html(html);
         $("#" + $_htmlTable.control_container).css ({
             border:"1px solid"
         })   
    };
    
    $(document).ready (function (){
        $_htmlTable.controls();
        $("#btn_add_table").click(function (){
            $_htmlTable.create();
        });
        
        $("div").click (function(event){
            if ($.inArray($(this).attr("id"),$_htmlTable.context)==0) { 
            //if ($(this).closest("div[contentEditable]").length>0) {
                $_htmlTable.insertMode = true;
                event.stopPropagation()
            }
            else {
                $_htmlTable.insertMode = false;
                
            }
         });
         
         $("#" + $_htmlTable.control_container + "_cols, #" + $_htmlTable.control_container + "_rows").keydown(function(event) {
            if ( event.keyCode == 46 || event.keyCode == 8 ) {
            // let it happen, don't do anything
            } else {
            // Ensure that it is a number and stop the keypress
                if (event.keyCode < 48 || event.keyCode > 57 ) {
                event.preventDefault(); 
                }   
            }
        });

         
    });
}


